/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName NetworkDetecterAndroid.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model
{
	
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.vo.NetworkInfoVO;
	
	import flash.net.InterfaceAddress;
	import flash.net.NetworkInfo;
	import flash.net.NetworkInterface;

	public class NetworkDetecterAndroid
	{
		
		public function NetworkDetecterAndroid()
		{
			
		}
		
		public function getNetworkInfo():void
		{
			
			var nerworkInfoVO:NetworkInfoVO = getNetWorkInfoVO();
			
			if(nerworkInfoVO)
			{
				
				try
				{
					
					updateNetworkInfo(nerworkInfoVO);
					
				}
				catch(error:DataBaseError)
				{
					
					throw error;
					
				}
				
			}
			else
			{
				
			}
			
		}
		
		private function getNetWorkInfoVO():NetworkInfoVO
		{
			
			var interfaces:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces();
			var networkInfoVO:NetworkInfoVO = null;
			
			for each(var net:NetworkInterface in interfaces)
			{
				
				if(net.active)
				{
					
					networkInfoVO = new NetworkInfoVO();
					for each ( var address:InterfaceAddress in net.addresses ) 
					{ 
						
						networkInfoVO.ipversion = address.ipVersion; 
						networkInfoVO.address = address.address; 
						networkInfoVO.broadcast_address = address.broadcast; 
						networkInfoVO.prefixlength = address.prefixLength.toString(); 
						
					} 
					
					networkInfoVO.active = net.active.toString();
					networkInfoVO.hardware_Address = net.hardwareAddress.toString();
					networkInfoVO.display_Name_Interface = net.displayName;
					networkInfoVO.mTU = net.mtu.toString();
					//net.name.toString();
				}
				
			}
			
			return networkInfoVO;
			
		}
		
		private function updateNetworkInfo(value:NetworkInfoVO):void
		{
			
			var dataBaseManager:IDatabaseManager = new DataBaseManager();
			//dataBaseManager.prepareTable("androidnetworkinfotable");
			
			try
			{
				
				dataBaseManager.insertValues("INSERT INTO IM_ANDROID_NETWORK_INFO(ani_interfaceName, ani_mTU, ani_display_Name_Interface, ani_active, ani_hardware_Address, ani_address, ani_broadcast_address, ani_ipversion, ani_prefixlength) VALUES('" +
					value.interfaceName + 
					"','" + value.mTU + 
					"','" + value.display_Name_Interface +
					"','" + value.active +
					"','" + value.hardware_Address +
					"','" + value.address +
					"','" + value.broadcast_address +
					"','" + value.ipversion +
					"','" + value.prefixlength + "')");
			}
			catch(error:DataBaseError)
			{
				
				throw error;
				
			}
			
		}
		
	}
	
}